﻿namespace PlugNT.Common
{
    using System;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;

    public class JScript
    {
        public static void Alert(string message)
        {
            string script = "<script language='javascript'>alert('{0}');</script>";
            HttpContext.Current.Response.Write(string.Format(script, message));
        }

        public static void Alert(object message, Page page)
        {
            string js = "<Script language='JavaScript'>\r\n                                alert('{0}');  \r\n                          </Script>";
            page.RegisterStartupScript("", string.Format(js, message));
        }

        public static void Alert(string message, Page page)
        {
            string script = "<script language='javascript'>alert('{0}');</script>";
            page.RegisterStartupScript("", string.Format(script, message));
        }

        public static void Alert(string str_Message, string str_Ctl_Name, Page page)
        {
            string js = "<script>alert('" + str_Message + "');document.forms(0)." + str_Ctl_Name + ".focus(); document.forms(0)." + str_Ctl_Name + ".select();</script>";
            page.RegisterStartupScript("", js);
        }

        public static void AlertAndRedirect(string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }

        public static void AlertAndRedirect(string message, string toURL, bool isTop)
        {
            string js;
            if (isTop)
            {
                js = "<script language=javascript>alert('{0}');window.top.location.replace('{1}')</script>";
                HttpContext.Current.Response.Write(string.Format(js, message, toURL));
            }
            else
            {
                js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
                HttpContext.Current.Response.Write(string.Format(js, message, toURL));
            }
        }

        public static void ConfirmAndRedirect(string message, string toURL, string oldURL)
        {
            string js = "<script language=javascript>if(confirm('" + message + "')){window.location.replace('" + toURL + "');}else{window.location.replace('" + oldURL + "');}</script>";
            HttpContext.Current.Response.Write(js);
        }

        public static string Help()
        {
            return "mailto:354990393@qq.com";
        }

        [Description(" 向客户端发送函数KendoPostBack(eventTarget, eventArgument)服务器端可接收__EVENTTARGET,__EVENTARGUMENT的值")]
        public static void JscriptSender(Page page)
        {
            page.RegisterHiddenField("__EVENTTARGET", "");
            page.RegisterHiddenField("__EVENTARGUMENT", "");
            string script = "\t\t\r\n                        <script language=Javascript>\r\n                        function doPostBack(eventTarget, eventArgument) \r\n                        {\r\n                            var theform = document.forms[0];\r\n                            theform.__EVENTTARGET.value = eventTarget;\r\n                            theform.__EVENTARGUMENT.value = eventArgument;\r\n                            theform.submit();\r\n                        }\r\n                        </script>";
            page.RegisterStartupScript("event", script);
        }

        public static void Location()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">\n");
            sb.Append("window.location.href=window.location.href;\n");
            sb.Append("</script>\n");
            HttpContext.Current.Response.Write(sb.ToString());
        }

        public static void RedirectPage(string url)
        {
            string path = url.StartsWith("http://") ? url : ("http://" + HttpContext.Current.Request.Url.Host + url);
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append(string.Format("window.location.href='{0}';", path));
            sb.Append("</script>");
            HttpContext.Current.Response.Write(sb.ToString());
        }

        public static void RedirectPage(string url, Page page)
        {
            string path = url.StartsWith("http://") ? url : ("http://" + HttpContext.Current.Request.Url.Host + url);
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append(string.Format("window.location.href='{0}';", path));
            sb.Append("</script>");
            page.RegisterStartupScript("", sb.ToString());
        }

        public static void ResponseScript(Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");
        }

        public static void ShowJS(string strCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">\n");
            sb.Append(strCode.Trim());
            sb.Append("\n</script>\n");
            HttpContext.Current.Response.Write(sb.ToString());
        }

        public static void ShowJS(string strCode, Page page)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">\n");
            sb.Append(strCode.Trim());
            sb.Append("\n</script>\n");
            page.Response.Write(sb.ToString());
        }

        public static void ShowRedirect(string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + str.Trim() + "\"); \n");
            sb.Append("var p=document.referrer; \n");
            sb.Append("window.location.href=p;\n");
            sb.Append("</script>");
            HttpContext.Current.Response.Write(sb.ToString());
        }

        public static void ShowRedirect(string str, Page page)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + str.Trim() + "\"); \n");
            sb.Append("var p=document.referrer; \n");
            sb.Append("window.location.href=p;\n");
            sb.Append("</script>");
            page.RegisterStartupScript("", sb.ToString());
        }
    }
}

